package ale.android.taxiDrivers;

import ale.android.R;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.Toast;

public class InsertTaxiDriver extends Activity {
	private EditText editTextName;
	private EditText editTextNick;
	private EditText editTextNationality;
	private Spinner spinnerCompany;
	private EditText editTextPhone;

	private RatingBar ranking;

	private Button buttonInsert;
	private Button buttonCancel;

	SQLiteDatabase db;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.insert_taxi_driver);

		initDB();
		initViews();

	}

	private void initViews() {

		editTextName = (EditText) findViewById(R.id.editTextName);
		editTextNick = (EditText) findViewById(R.id.editTextNick);
		editTextNationality = (EditText) findViewById(R.id.editTextNationality);

		spinnerCompany = (Spinner) findViewById(R.id.spinnerCompany);
		ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this,
				R.array.company, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerCompany.setAdapter(adapter);

		editTextPhone = (EditText) findViewById(R.id.editTextPhone);
		ranking = (RatingBar) findViewById(R.id.ratingBar);

		buttonInsert = (Button) findViewById(R.id.buttonInsertTaxiDriver);
		buttonInsert.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				if (dadesOk()) {
					db.execSQL("INSERT INTO TaxiDrivers (Name, Nick, Nationality, Company, NumberOfTrips, Phone, Ranking) "
							+ "VALUES ('"
							+ editTextName.getText().toString()
							+ "',  '"
							+ editTextNick.getText().toString()
							+ "',  '"
							+ editTextNationality.getText().toString()
							+ "',  '"
							+ spinnerCompany.getSelectedItem().toString()

							+ "',  '1', '"
							+ editTextPhone.getText().toString()
							+ "',  '" + (int) ranking.getRating() * 2 + "')");

					// Falta comprobar les primary key? DB treu exepcio?

					finish();
				} else {
					Toast.makeText(getApplicationContext(), "Incorrect data",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
		buttonCancel = (Button) findViewById(R.id.buttonCancelTaxiDriver);
		buttonCancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				finish();
			}
		});

	}

	private void initDB() {
		TaxiDriversSQLiteHelper usdbh = new TaxiDriversSQLiteHelper(this,
				"DBTaxiDrivers", null, 1);
		db = usdbh.getWritableDatabase();
	}

	private boolean dadesOk() {

		if (editTextName.getText().toString().equals(""))
			return false;
		if (editTextNick.getText().toString().equals(""))
			return false;
		if (editTextNationality.getText().toString().equals(""))
			return false;
		if (spinnerCompany.getSelectedItem().toString().equals(""))
			return false;
		if (editTextName.getText().toString().equals(""))
			return false;

		return true;
	}
}
